package com.pos.clothes.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.pos.clothes.pojo.Actor;
import com.pos.clothes.service.UserService;
import com.pos.clothes.pojo.User;
import com.pos.clothes.util.JsonData;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.*;

@Controller
@RequiresPermissions("4")
//@RequiresPermissions(value = {"4","3"},logical = Logical.OR)
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService service;
    @Autowired
    private User user;

    @RequestMapping("/showList")
    @ResponseBody
    public Object showActor(@RequestParam("pageNum") int pageNum,
                            @RequestParam("pageSize") int pageSize) throws Exception{
        PageInfo res = service.getUser(pageNum,pageSize);
        return res;
    }

    @RequestMapping("/showActor")
    @ResponseBody
    public List<Actor> showActor(){

        List<Actor> actors = service.showActor();
        return actors;
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public Object insertUser(@RequestBody Map<String,Object> map){
        Object user1 =  map.get("user");
        ObjectMapper mapper = new ObjectMapper();
        User user = mapper.convertValue(user1, new TypeReference<User>() { });
        int res = service.insertUser(user);
        if(res == -1){
            return JsonData.buildError("该工号已存在。",-1);
        }
        else{
            return  JsonData.buildSuccess(res,"ok");
        }
    }

    @RequestMapping("/searchUser")
    @ResponseBody
    public User insertUser(@RequestParam String gonghao){

        return  service.searchUser(gonghao);
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public Object deleteUser(@RequestParam Integer iduser){

        int res = service.deleteUser(iduser);
        return JsonData.buildSuccess(res,"ok");
    }


    @RequestMapping(value = "/x", method = RequestMethod.GET)
    @ResponseBody
    public User ce(@RequestParam String username) {
        return service.getByUsername(username);
    }


}
